package com.chester.quickstarter.exception;

import com.alibaba.fastjson.JSONObject;
import com.chester.quickstarter.util.common.JsonResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author chester7cheng
 */
@ControllerAdvice
@ResponseBody
public class GlobalDefaultExceptionHandler {
    private final static Logger logger = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);

    @ExceptionHandler
    public JSONObject exceptionHandler(Exception e){
        e.printStackTrace();
        logger.error("exceptionHandler: " + e.getMessage());

        if(e instanceof ParamException){
            ParamException paramException=(ParamException)e;
            return JsonResultUtil.generateFailJson(paramException.getMessage());
        }else if(e instanceof ServerException){
            ServerException serverException=(ServerException)e;
            return JsonResultUtil.generateFailJson(serverException.getMessage());
        }else{
            return JsonResultUtil.generateFailJson(e.getMessage());
        }
    }
}
